package cn.jbolt.ai.core.chain.config;

import cn.hutool.core.util.StrUtil;
import cn.jbolt.util.JSONUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.jboltai.config.AIParamKey;
import com.jboltai.resource.ai.AIModel;

public class LlmConfig  {
    /**
     * 输出的字段名
     */
    protected String outputField;

    /**
     * 输入字段
     */
    private String inputField;

    /**
     * 格式化
     */
    private String format = "markdown";

    /**
     * 是否输出到响应
     */
    private boolean outputToResponse = false;
    /**
     * prompt模板
     */
    private String promptTpl;

    private Integer maxTokens = 2000;

    private String model;

    @JsonIgnore
    private AIModel modelType;

    private String defaultModel;

    /**
     * 是否流式输出
     */
    private boolean stream = false;

    /**
     * 是否使用上下文
     */
    private boolean useContext = false;

    private Double topP;

    private Integer topK;

    private Double temperature = 0.6;

    private String systemRole = null;

    /**
     * 消息是否持久化到数据库
     */
    private boolean persistDb = false;


    public static LlmConfig from(JsonNode jsonNode) {
        LlmConfig config = JSONUtil.toObject(jsonNode, LlmConfig.class);
        if (config == null) return null;
        if (jsonNode.has(AIParamKey.MODEL)) {

            String model = jsonNode.get(AIParamKey.MODEL).asText();
            String[] items = model.split(":", 2);
            config.setModelType(AIModel.get(items[0]));
            if (items.length > 1 && StrUtil.isNotEmpty(items[1])) {
                config.setDefaultModel(items[1]);
            }
        }


        return config;
    }

    //  转换为 JSON 字符串的方法
    public static JsonNode getDefaultConfig() {

        return JSONUtil.toJsonObject(new LlmConfig());
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getInputField() {
        return inputField;
    }

    public void setInputField(String inputField) {
        this.inputField = inputField;
    }

    public boolean isPersistDb() {
        return persistDb;
    }

    public void setPersistDb(boolean persistDb) {
        this.persistDb = persistDb;
    }

    public String getPromptTpl() {
        return promptTpl;
    }

    public void setPromptTpl(String promptTpl) {
        this.promptTpl = promptTpl;
    }


    public AIModel getModelType() {
        return modelType;
    }

    public void setModelType(AIModel modelType) {
        this.modelType = modelType;
    }

    public String getDefaultModel() {
        return defaultModel;
    }

    public void setDefaultModel(String defaultModel) {
        this.defaultModel = defaultModel;
    }


    public Integer getMaxTokens() {
        return maxTokens;
    }

    public void setMaxTokens(Integer maxTokens) {
        this.maxTokens = maxTokens;
    }

    public boolean isUseContext() {
        return useContext;
    }

    public void setUseContext(boolean useContext) {
        this.useContext = useContext;
    }

    public Double getTopP() {
        return topP;
    }

    public void setTopP(Double topP) {
        this.topP = topP;
    }

    public Integer getTopK() {
        return topK;
    }

    public void setTopK(Integer topK) {
        this.topK = topK;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    public boolean isStream() {
        return stream;
    }

    public void setStream(boolean stream) {
        this.stream = stream;
    }

    public boolean isOutputToResponse() {
        return outputToResponse;
    }

    public void setOutputToResponse(boolean outputToResponse) {
        this.outputToResponse = outputToResponse;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getSystemRole() {
        return systemRole;
    }

    public void setSystemRole(String systemRole) {
        this.systemRole = systemRole;
    }

    public String getOutputField() {
        return outputField;
    }

    public void setOutputField(String outputField) {
        this.outputField = outputField;
    }

    public String getModel() {
        return model;
    }


}
